<?
/**
**智游宝对接
**2015-06-08 pm 14:45
**user by pzp
**/
class ZhiYouBao{
	private $zyb_URL = 'boss.zhiyoubao.com/boss/service/code.htm';
	public function __construct($apply_did){
		switch ($apply_did){		
			case 3311:  //洛阳市中旅旅行社有限公司
				$this->user = "hlwh";
				$this->key = "sdzfxhlwh";
				$this->corpcode = 'sdzfxhlwh';
			break;				
			case 22415: //小票虫网络科技
				$this->user = "sdhyls";
				$this->key = "sdzfxsdhyls";
				$this->corpcode = 'sdzfxsdhyls';
			break;	
			case 37809:  //南山大佛
				$this->user = "lypw";
				$this->key = "sdzfxlypw";
				$this->corpcode = 'sdzfxlypw';
			break;			
			case 113:  //测试账号
				$this->user = 'admin';
				$this->key = 'TESTFX';
				$this->zyb_URL = 'lxh.sendinfo.com.cn/boss/service/code.htm';
			break;
			case 51667:
				$this->user = 'xmgzl';
				$this->key = '3665D15A9D73DF3506F51CFD013ABC3B';
				$this->corpcode = 'sdzfxxmgzl';
			break;
			case 50618: //驴妈妈
				$this->user = 'lvmamafx';
				$this->key = 'lvmamafx';
				$this->corpcode = 'lvmamafx';
			break;
			case 22845: //乐游票付通 
				$this->user = 'xmlyjr';
				$this->key = 'sdzfxxmlyjr';
				$this->corpcode = 'sdzfxxmlyjr';
			break;			
			case 4274: //携程分销
				$this->user = 'shxcfx';
				$this->key = 'xfcxhs';
				$this->corpcode = 'shxcfx';
			break;
			
			case 27810: //先行网运营部
				$this->user = 'yksty';
				$this->key = '6265C757118A935909641669CF12B212';
				$this->corpcode = 'sdzfxyksty';
			break;			
			
			case 38270: //烟台华泰国
				$this->user = 'ytht';
				$this->key = 'sdzfxytht';
				$this->corpcode = 'sdzfxytht';
			break;			
			
			case 58776: //洛阳惠旅文化传播有限公司
				$this->user = 'hdjd';
				$this->key = 'sdzfxhdjd';
				$this->corpcode = 'sdzfxhdjd';
			break;
			
			case 7043: //泰宁县大金湖国际旅行社有限公司
				$this->user = 'sqxlx';
				$this->key = 'CE3FDC901B1A87B668F336A107906069';
				$this->corpcode = 'sdzfxsqxlx';
			break;
			
			case 3418: //青岛新锐
				$this->user = 'qdyd';
				$this->key = 'sdzfxqdyd';
				$this->corpcode = 'sdzfxqdyd';
			break;
					
			default:
			break; 
		}
	}
	
	// private $user = 'admin';
	// private $key = 'TESTFX';
	// private $productid = '20140710012604';
	
	//提交订单
	public function zyb_submit_order($request_array){
		$requestTime = $request_array['UUordertime'];
		$orderPrice = $request_array['UUtprice'] * $request_array['UUtnum'];
		$submit_xml =<<<XML
<PWBRequest>
<transactionName>SEND_CODE_REQ</transactionName>
<header>
	<application>SendCode</application>
	<requestTime>{$requestTime}</requestTime>
</header>
<identityInfo>
	<corpCode>{$this->corpcode}</corpCode>
	<userName>{$this->user}</userName>
</identityInfo>
<orderRequest>
	<order>
		<certificateNo>{$request_array['IDcar']}</certificateNo>
		<linkName>{$request_array['pftordername']}</linkName>
		<linkMobile>{$request_array['UUordertel']}</linkMobile>
		<orderCode>{$request_array['pftordernum']}</orderCode>
		<orderPrice>{$orderPrice}</orderPrice>
		<groupNo></groupNo>
		<payMethod>vm</payMethod>
		<ticketOrders>
			<ticketOrder>
				<orderCode>{$request_array['pftordernum']}</orderCode>
				<price>{$request_array['UUtprice']}</price>
				<quantity>{$request_array['UUtnum']}</quantity >
				<totalPrice>{$orderPrice}</totalPrice>
				<occDate>{$request_array['UUplaytime']}</occDate>
				<goodsCode>{$request_array['uuid']}</goodsCode>
				<goodsName>{$request_array['UUltitle']}</goodsName>
				<remark></remark>
			</ticketOrder>
		</ticketOrders>
	</order>
</orderRequest>
</PWBRequest>
XML;
		// $this->logfile($submit_xml,'zyb_submit_fail');	
		$sign = $this->sign($submit_xml);
		$request = $this->zyb_post($submit_xml,$sign);
		$request_xml = simplexml_load_string($request);
		if((int)$request_xml->code == 0){
			$this->logfile($request_array['pftordernum']."|".(string)$request_xml->orderResponse->order->orderCode,'zyb_submit_success');
			$this->send_sms($request_array['pftordernum']);
			return $request_xml;
		}
		else{
			$this->logfile($request_array['pftordernum']."|".(int)$request_xml->code."|".(string)$request_xml->description,'zyb_submit_fail');
			return 101;
		}
	}
	
	//发短信
	public function send_sms($order_true){
		$requestTime = date('Y-m-d');
		$submit_xml = <<<XML
<PWBRequest>
	<transactionName>SEND_SM_REQ</transactionName>
	<header>
		<application>SendCode</application>
		<requestTime>{$requestTime}</requestTime>
	</header>
	<identityInfo>
		<corpCode>{$this->corpcode}</corpCode>
		<userName>{$this->user}</userName>
	</identityInfo>
	<orderRequest>
		<order>
		<orderCode>{$order_true}</orderCode>
		<tplCode></tplCode>
		</order>
	</orderRequest>
</PWBRequest>
XML;
		$sign = $this->sign($submit_xml);
		$request = $this->zyb_post($submit_xml,$sign);
		$request_xml = simplexml_load_string($request);
		if((int)$request_xml->code == 0){
			$this->logfile($order_true."|".(int)$request_xml->code,'zyb_sendsms_success');
		}
		else{
			$this->logfile($order_true."|".(int)$request_xml->code."|".(string)$request_xml->description,'zyb_sendsms_fail');
		}
	}	
	
	//修改订单
	public function zyb_modify($PFTOrdern,$rnum){
		$requestTime = date('Y-m-d');
		$submit_xml=<<<XML
<PWBRequest>
	<transactionName>RETURN_TICKET_NUM_NEW_REQ</transactionName>
	<header>
		<application>SendCode</application>
		<requestTime>{$requestTime}</requestTime>
	</header>
	<identityInfo>
		<corpCode>{$this->corpcode}</corpCode>
		<userName>{$this->user}</userName>
	</identityInfo>
	<orderRequest>
		<returnTicket>
		<orderCode>{$PFTOrdern}</orderCode>
		<returnNum>{$rnum}</returnNum>
		<thirdReturnCode>{$PFTOrdern}</thirdReturnCode>
		</returnTicket>
	</orderRequest>
</PWBRequest>
XML;
		$sign = $this->sign($submit_xml);
		$request = $this->zyb_post($submit_xml,$sign);
		$request_xml = simplexml_load_string($request);
		if((int)$request_xml->code == 0){
			$this->logfile($PFTOrdern."|".(int)$request_xml->code,'zyb_modifying_success');
			return 100;
		}
		else{
			$this->logfile($PFTOrdern."|".(int)$request_xml->code."|".(string)$request_xml->description,'zyb_modifying_fail');
			return 101;
		}		
	}
	//取消订单
	public function zyb_cancel($tempOrder){	
		$requestTime = date('Y-m-d');
		$submit_xml=<<<XML
<PWBRequest>
	<transactionName>SEND_CODE_CANCEL_NEW_REQ</transactionName>
	<header>
		<application>SendCode</application>
		<requestTime>{$requestTime}</requestTime>
	</header>
	<identityInfo>
		<corpCode>{$this->corpcode}</corpCode>
		<userName>{$this->user}</userName>
	</identityInfo>
	<orderRequest>
		<order>
			<orderCode>{$tempOrder}</orderCode>
		</order>
	</orderRequest>
</PWBRequest>
XML;
		$sign = $this->sign($submit_xml);
		$request = $this->zyb_post($submit_xml,$sign);
		$request_xml = simplexml_load_string($request);
		if((int)$request_xml->code == 0){
			$this->logfile($tempOrder."|".(int)$request_xml->code,'zyb_canceling_success');
			return 100;
		}
		else{
			$this->logfile($tempOrder."|".(int)$request_xml->code."|".(string)$request_xml->description,'zyb_canceling_fail');
			return 101;
		}
	}
	
	
	//加密方式
	public function sign($xml){
		$sign = md5("xmlMsg=".$xml.$this->key);
		return $sign;
	}
	
	//提交
	public function zyb_post($xml,$sign){
		$post_data = array(); 
		$post_data['xmlMsg'] = $xml; 
		$post_data['sign'] = $sign;
		$o=""; 		
		foreach ($post_data as $k=>$v){ 
			$o.= "$k=".$v."&"; 
		} 
		$post_data=substr($o,0,-1); 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL,$this->zyb_URL); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);		
		$reponse = curl_exec($ch);
		curl_close($ch);
		return $reponse;
	}	
	
	//日志	
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt';
		if(file_exists($filepath)){
			$word = date("Y-m-d H:i:s")."|".$word;
			// $word = mb_convert_encoding($word, 'GBK', 'UTF-8');
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			{
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
				$handler=fopen($filepath,'w');
				flock($handler, LOCK_EX) ;
				fwrite($handler,"\n".$word);
				flock($handler, LOCK_UN);
				fclose($handler);
			}
			else
			{
				$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
				flock($fp, LOCK_EX);
				fwrite($fp,$word."\n");
				flock($fp, LOCK_UN);
				fclose($fp);
			}
		}else{
			$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}
}
// $zyb = new ZhiYouBao();
// $sendcode = $zyb->zyb_submit_order();
// print_r($sendcode);
// $txt = simplexml_load_string($txt);
// print_r($txt->code);
// print_r($txt->orderResponse->order->orderCode);
/* 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步,
1. 发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据
2. 接收到Server返回的100-continue应答以后, 才把数据POST给Server
*/
?>
}else{
			$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}
}
// $zyb = new ZhiYouBao();
// $sendcode = $zyb->zyb_submit_order();
// print_r($sendcode);
// $txt = simplexml_load_string($txt);
// print_r($txt->code);
// print_r($txt->orderResponse->order->orderCode);
/* 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步,
1. 发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据
2. 接收到Server返回的100-continue应答以后, 才把数据POST给Server
*/
?>
